URL = "http://curl.haxx.se/download/curl-%(VERSION)s.tar.gz"
VERSIONS = ["7.20.1", "7.21.3"]
DEFAULT_VERSION = "7.20.1"
DEPENDS = ["zlib", "openssl"]

from source.builders import CMakeBuilder
import os

class CurlBuilder( CMakeBuilder ):
  """ subclass of ConfigureMakeBuilder with custom configure step """

  def configure( self ):
    zlibdep = self.dependencies["zlib"]
    openssldep = self.dependencies["openssl"]
    incdir = " -DZLIB_INCLUDE_DIR="+zlibdep.getincludedir()
    lib = " -DZLIB_LIBRARY="+zlibdep.getlibdir()+os.sep+{ "Linux" : "libz.so ", "Windows" : "zlib.lib " }[self.machine.platform]
    prefixpath = " -DCMAKE_PREFIX_PATH="+openssldep.targetpath()
    CMakeBuilder.configure(self, incdir+lib+prefixpath)    
  
  def make_devenv(self):
    if self.debug:
      self.execute( "cmake --build . --config Debug" )
    else:
      self.execute( "cmake --build . --config Release" )  

  def makeUnixMakefiles(self):
    self.execute( "make -j " + str(self.machine.cores) )

  def install(self):
    os.chdir(self.extractedpath)
    bindir = self.installpath() + "/bin"
    libdir = self.installpath() + "/lib"
    includedir = self.installpath() + "/include"
    self.copydir("include", includedir )

    if not self.debug:
      if self.machine.isWindows():
        self.copyfile( "lib/libcurl.dll", bindir )
        self.copyfile( "lib/libcurl_imp.lib", libdir )
      else:
        self.copyfile( "lib/libcurl.so", libdir )

#todo support building debug
BUILDER = CurlBuilder

